package p16.code;

import java.math.BigInteger;
import java.util.concurrent.BlockingQueue;

/**
 * 通过中断机制取消任务
 */
public class PrimeProducer extends Thread{

    private final BlockingQueue<BigInteger> queue;

    public PrimeProducer (BlockingQueue<BigInteger> queue){
        this.queue = queue;
    }

    public void run() {
        try{
            BigInteger p = BigInteger.ONE;
            while(!Thread.currentThread().isInterrupted()){
                queue.put(p=p.nextProbablePrime());
            }
        }catch(InterruptedException ignored){
        }
    }

    public void cancel(){
        interrupt();//使用中断
    }

}
